// pages/order/order.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["全部", "待支付", "待发货", "待收货","已完成"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    order_list: [],
    slider_width: 0,
    disabled:false
  },
  tabClick: function (e) {
    var value = e.currentTarget.dataset.value
    var order_list = this.data.order_list
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    switch (value) {
      case "全部":
        app.ajax({
          url: app.host + "mall/order",
          success: function (res) {
            wx.hideLoading()
            order_list = res.data.data.list
            that.setData({
              order_list: order_list
            })
          }
        })
        break;

      case "待支付":
        app.ajax({
          url: app.host + "mall/order?status=1",
          success: function (res) {
            wx.hideLoading()
            order_list = res.data.data.list
            that.setData({
              order_list: order_list
            })
          }
        })
        break;

      case "待发货":
        app.ajax({
          url: app.host + "mall/order?status=2",
          success: function (res) {
            wx.hideLoading()
            order_list = res.data.data.list
            that.setData({
              order_list: order_list
            })
          }
        })
        break;

      case "待收货":
        app.ajax({
          url: app.host + "mall/order?status=3",
          success: function (res) {
            wx.hideLoading()
            order_list = res.data.data.list
            that.setData({
              order_list: order_list
            })
          }
        })
        break;

      case "已完成":
        app.ajax({
          url: app.host + "mall/order?status=4",
          success: function (res) {
            wx.hideLoading()
            order_list = res.data.data.list
            that.setData({
              order_list: order_list
            })
          }
        })
        break;
    }
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id,
    });
  },
  repay: function (e) {
    var that = this
    var order_id = e.currentTarget.dataset.order_id
    var order_list = this.data.order_list
    var disabled = this.data.disabled
    this.setData({
      disabled:true
    })
    app.ajax({
      url: app.host + "mall/order/repay?order_id=" + order_id,
      success:function(res){
        var parameters = res.data.data.parameters
        parameters.success=function(){
          wx.showToast({
            title: '支付成功',
          })
          for (let i in order_list){
            if (order_list[i].order_id = order_id){
              order_list[i].order_status = 20
            }
          }
          that.setData({
            order_list:order_list
          })
        }
        parameters.complete = function(){
          that.setData({
            disabled: false
          })
        }
        if(res.code==0){
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success:function(msg){
              if (msg.confirm){
                wx.requestPayment(parameters)
              }
            }
          })
        }else{
          wx.requestPayment(parameters)
        }
      }
    })
  },
  cancel: function (e) {
    var ispay = e.currentTarget.dataset.ispay
    var that = this
    var order_id = e.currentTarget.dataset.order_id
    var order_list = this.data.order_list
    if(ispay){
      wx.showModal({
        title: '确定取消该订单吗？',
        success:function(res){
          if(res.confirm){
            app.ajax({
              url: app.host + "mall/order/cancel?order_id=" + order_id,
              success: function () {
                for (let i in order_list) {
                  if (order_list[i].order_id == order_id) {
                    order_list[i].order_status = 10
                    break;
                  }
                }
                that.setData({
                  order_list: order_list
                })
                wx.showToast({
                  title: '取消成功',
                })
              }
            })
          }
        }
      })
    }else{
      wx.showModal({
        title: '取消订单',
        content: '取消订单请拨打客服电话，是否拨打？',
        success: function (res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: '0871-63561965',
            })
          }
        }
      })
    }
  },
  finish: function (e) {
    var that = this
    var order_id = e.currentTarget.dataset.order_id
    app.ajax({
      url: app.host + 'mall/order/finish?order_id=' + order_id,
      success: function (res) {
        wx.showToast({
          title: '确认成功',
        })
        var order_list = that.data.order_list
        for (let i in order_list){
          if (order_list[i].order_id = order_id){
            order_list[i].order_status = 100
          }
        }
        that.setData({
          order_list: order_list
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.options = options
    var that = this
    wx.createSelectorQuery().select('.weui-navbar__item').boundingClientRect(function (res) {
      that.setData({
        slider_width: res.width
      })
    }).exec()
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var options = this.options
    wx.showLoading({
      title: '加载中',
    })
    app.ajax({
      url: app.host + (options.index ? "mall/order?status=" + options.index:"mall/order"),
      success: function (res) {
        // console.log(res.data.data.list)
        wx.hideLoading()
        wx.getSystemInfo({
          success: function (msg) {
            var sliderOffset = msg.windowWidth / that.data.tabs.length * options.index
            that.setData({
              activeIndex: options.index,
              sliderOffset: sliderOffset,
              order_list: res.data.data.list
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})